package com.fyy.scheduled.controller;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author fyy
 * @Description
 * @Date 2020/12/18 9:24
 */
@Controller
@RequestMapping(value = "/task/")
public class ScheduledController {


    @RequestMapping(value = "test")
    public void test(){
        System.out.println("定时任务测试");
    }

    /**
     * @Author fyy
     * @Description fixedRate：固定速率执行,单位(ms)，例如每5秒执行一次
     * @Date 2020/12/18 9:27
     */
//    @Scheduled(fixedRate = 5000)
    public void fixeRateTask(){
        System.out.println("Fixed Rate Task，Current Thread : " + Thread.currentThread().getName());
    }

    /**
     * @Author fyy
     * @Description fixedDelay：固定延迟执行,单位(ms)，例如距离上一次调用成功后2秒执行
     * @Date 2020/12/18 9:27
     */
    @Scheduled(fixedDelay = 2000)
    public void fixedDelayTask() throws InterruptedException {
        Thread.sleep(20000);
        System.out.println("Fixed Delay Task，Current Thread : " + Thread.currentThread().getName());
    }

    /**
     * @Author fyy
     * @Description initialDelay：初始延迟任务，例如任务开启过5秒后再执行，之后以固定频率或者间隔执行
     * @Date 2020/12/18 9:27
     */
//    @Scheduled(initialDelay = 5000,fixedRate = 5000)
    public void initialDelayTask(){
        System.out.println("Initial Delay Task，Current Thread : " + Thread.currentThread().getName());
    }

    /**
     * @Author fyy
     * @Description cron：使用 Cron 表达式执行定时任务
     * @Date 2020/12/18 9:27
     */
//    @Scheduled(cron = "*/6 * * * * ?")
    public void cronTask(){
        System.out.println("Cron Task，Current Thread : " + Thread.currentThread().getName());
    }

}
